//
//  WWListenListViewController.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/7.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWListenListViewController.h"
#import "AFTools.h"
#import "WWListenListTableViewCell.h"
#import "WWListenListModel.h"
#import "AlbumFViewController.h"
#import "WHNTableViewCell.h"
#import "WHNList.h"
#import "PublicHeader.h"
#import <UIImageView+WebCache.h>
#import "FPlayViewController.h"

@interface WWListenListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *introLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UILabel *bottomLabel;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, assign) BOOL type;

@end
static NSString *identifier = @"identifier";
@implementation WWListenListViewController

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
        [_tableView registerClass:[WWListenListTableViewCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerClass:[WHNTableViewCell class] forCellReuseIdentifier:identifier];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = FitConstant_YorHeight(82);
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)handelData {
    [AFTools getDataByURLString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?id=%ld&device=iPhone", self.specialId] WithDataBlock:^(id data) {
        NSArray *tempArray = [NSArray arrayWithArray:[data objectForKey:@"list"]];
        //专辑
        if ([[[data objectForKey:@"info"] objectForKey:@"contentType"]  isEqual: @1]) {
            for (NSDictionary *dic in tempArray) {
                WWListenListModel *list = [WWListenListModel wwModelWithDictionary:dic];
                [self.dataArray addObject:list];
            }
            _type = NO;
        }
        //音乐
        else{
            [_dataArray addObjectsFromArray:[data objectForKey:@"list"]];
            _type = YES;
        }
        NSDictionary *headerDic = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"info"]];
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2 - FitConstant_YorHeight(40))];
        self.headerImageView = [[UIImageView alloc] init];
        self.headerImageView.frame = CGRectMake(FitConstant_XorWidth(8), FitConstant_YorHeight(12), FitConstant_XorWidth(40), FitConstant_YorHeight(50));
        self.headerImageView.image = [UIImage imageNamed:@"sanjiao.png"];
        [self.headerView addSubview:self.headerImageView];
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.frame = CGRectMake(FitConstant_XorWidth(50), FitConstant_YorHeight(10), self.view.frame.size.width - FitConstant_XorWidth(60), FitConstant_YorHeight(60));
        _titleLabel.text = [headerDic objectForKey:@"title"];
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = [UIFont systemFontOfSize:21];
        [self.headerView addSubview:self.titleLabel];
        _introLabel = [[UILabel alloc] init];
        _introLabel.frame = CGRectMake(FitConstant_XorWidth(100), FitConstant_YorHeight(70), FitConstant_XorWidth(175), FitConstant_YorHeight(15));
        _introLabel.text = @"———◇ 简 介 ◇———";
        _introLabel.font = [UIFont systemFontOfSize:16];
        _introLabel.textColor = [UIColor grayColor];
        [self.headerView addSubview:_introLabel];
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.frame = CGRectMake(FitConstant_XorWidth(8), FitConstant_YorHeight(88), self.view.frame.size.width, FitConstant_YorHeight(110));
        _contentLabel.font = [UIFont systemFontOfSize:15];
        _contentLabel.text = [headerDic objectForKey:@"intro"];
        _contentLabel.numberOfLines = 0;
        [self.headerView addSubview:self.contentLabel];
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.frame = CGRectMake(self.view.frame.size.width - FitConstant_XorWidth(120), FitConstant_YorHeight(200), FitConstant_XorWidth(120), FitConstant_YorHeight(20));
        _bottomLabel.text = [NSString stringWithFormat:@"小编  %@", [headerDic objectForKey:@"nickname"]];
        _bottomLabel.font = [UIFont systemFontOfSize:16];
        [self.headerView addSubview:_bottomLabel];
        [self.headerView NightWithType:UIViewColorTypeNormal];
        self.tableView.tableHeaderView = self.headerView;
        [self.titleLabel NightWithType:UIViewColorTypeNormal];
        [self.introLabel NightWithType:UIViewColorTypeNormal];
        [self.contentLabel NightWithType:UIViewColorTypeNormal];
        [self.bottomLabel NightWithType:UIViewColorTypeNormal];
        [self.tableView reloadData];
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    [self handelData];
    [self.tableView NightWithType:UIViewColorTypeNormal];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_type) {
        WWListenListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (_dataArray.count != 0) {
            [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:[_dataArray[indexPath.row] objectForKey:@"coverSmall"]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
            cell.titleLabel.text = [_dataArray[indexPath.row] objectForKey:@"title"];
            cell.introLabel.text = [NSString stringWithFormat:@" by %@", [_dataArray[indexPath.row] objectForKey:@"nickname"]];
            CGFloat a = [[_dataArray[indexPath.row] objectForKey:@"playsCounts"] integerValue] / 10000 * 1.0;
            if (a >= 1) {
                cell.trackLabel.text = [NSString stringWithFormat:@"%.1f万", a];
            }
            else {
                cell.trackLabel.text = [NSString stringWithFormat:@"%@", [_dataArray[indexPath.row] objectForKey:@"playsCounts"]];
            }
            cell.trackImageView.image = [UIImage imageNamed:@"sanjiao1.png"];
        }
        return cell;
    }
    else{
        WWListenListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        WWListenListModel *model = self.dataArray[indexPath.row];
        cell.model = model;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_type) {
        FPlayViewController *playVC = [[FPlayViewController alloc]init];
        playVC.trackID = [NSString stringWithFormat:@"%@", [_dataArray[indexPath.row] objectForKey:@"id"]];
        playVC.trackUid = [NSString stringWithFormat:@"%@", [_dataArray[indexPath.row] objectForKey:@"uid"]];
//        playVC.albumId = [NSString stringWithFormat:@"%@", [_dataArray[indexPath.row] objectForKey:@"coverSmall"]];
        [self.navigationController pushViewController:playVC animated:YES];
    }
    else{
        AlbumFViewController *vc = [[AlbumFViewController alloc] init];
        WWListenListModel *model = self.dataArray[indexPath.row];
        vc.track = [model.track integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
